// // app.js
const API = require('./api/axios')
const COMM = require('./common/comm')

App({
  globalData: {
    API: require('./api/axios'),
    COMM: require('./common/comm'),
  },
  API,
  COMM,
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    let token = wx.getStorageSync('token')
    if (null == token || token == '') {
      // 登录
      wx.login({
        success: res => {
          console.log(`===res.code:${res.code}=====`)
          if (res.code) {
            API.post(COMM.apiList.WxLogin, { code: res.code }, 'form').then(
              data => {
                wx.setStorageSync('token', data.token)
                console.log("==== login success ===")
                wx.reLaunch({ url: '/pages/index/index' })
              }
            ).catch(err => {
              console.log("请求失败:", err);
              wx.reLaunch({ url: '/pages/user/index' })
            });
          }
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
        }
      })
    }

  }
})
